<?php
$articlerServices = new ArticleService();
$articlerpriceServices = new ArticlepriceService();
switch ($action) {
	case "add":
		if($permit == true)
		{
			$data = array();
			$data1 = array();
			$data2 = array();
			
			$uploaddir = "./images/imgArticle/";
			$uploaddirfile = "./images/drawingFile/";
			if(isset($_FILES))
			{
				if(isset($_POST['code']) && $_POST['code'] != '')
				{
					$folder = $_POST['code'];
					mkdir($uploaddir.$folder);
					chmod($uploaddir.$folder, "0777");
					mkdir($uploaddirfile.$folder);
					chmod($uploaddirfile.$folder, "0777");
					foreach ($_FILES as $key => $file) {
						$data[$key] = $file;
					}
					for($i=0;$i<4;$i++)
					{
						$nameImg = $data['imagePath'.$i]['name'];
						$typeImg = $data['imagePath'.$i]['type'];
						$sizeImg = $data['imagePath'.$i]['size'];
						if($sizeImg.$i > 5120000)
						{
							echo "<script>alert('Size '.$nameImg.$i.' is max');</script>";
						}
						elseif($nameImg != '')
						{
							if(($typeImg =='image/bmp' || $typeImg == 'image/jpeg' || $typeImg =='image/gif'|| $typeImg =='image/pjpeg' || $typeImg =='image/png' || $typeImg =='image/jpg' ))
							{
								$now = date('YmdHis');
								$photo = strstr($nameImg,".");
								$pic = $now.$i.$photo;
								$file = $uploaddir . $folder . "/" . $pic;
								move_uploaded_file($data['imagePath'.$i]['tmp_name'], $file);
								$data1['imagePath'.$i] = $pic;
							}
							else
							{
								echo "<script>alert('File ' .$nameImg.$i.' is not valid');</script>";
							}
						}
						else
							$data1['imagePath'.$i] = '';
					}
					$nameFile = $data['specificationFile']['name'];
					$typeFile = $data['specificationFile']['type'];
					$sizeFile = $data['specificationFile']['size'];
					if($sizeFile > 10240000)
					{
						echo "<script>alert('Size '.$nameFile .' is max');</script>";
					}
					elseif($nameFile != '')
					{				
						$now = date('YmdHis');
						$photo = strstr($nameFile,".");
						$pic = $now.$i.$photo;
						$file = $uploaddirfile.$folder . "/" . $pic;
						move_uploaded_file($data['specificationFile']['tmp_name'], $file);
						$data1['specificationFile'] = $pic;
					}
				}
			}
			foreach ($_POST as $key => $value) {
				$data1[$key] = $value;
			}
			$data1['inPriceUSD'] = Tool::convertMoneyToNumber($data1['inPriceUSD']);
			$data1['priceUSD'] = Tool::convertMoneyToNumber($data1['priceUSD']);
			$data1['inPriceEUR'] = Tool::convertMoneyToNumber($data1['inPriceEUR']);
			$data1['priceEUR'] = Tool::convertMoneyToNumber($data1['priceEUR']);
			
			$data2['articleID'] = $_POST['code'];
			$data2['buyPriceUSD'] = Tool::convertMoneyToNumber($data1['inPriceUSD']);
			$data2['sellPriceUSD'] = Tool::convertMoneyToNumber($data1['priceUSD']);
			$data2['buyPriceEUR'] = Tool::convertMoneyToNumber($data1['inPriceEUR']);
			$data2['sellPriceEUR'] = Tool::convertMoneyToNumber($data1['priceEUR']);
		
			$articlepriceDomain = new ArticlepriceDomain($data2);
			
			$data1['cbm'] = $data1['width'] * $data1['height'] * $data1['depth'];			
			if($data1['whdUnit'] == 'dm')
				$data1['cbm'] = $data1['cbm']/1000;
			if($data1['whdUnit'] == 'cm')
				$data1['cbm'] = $data1['cbm']/1000000;
			if($data1['whdUnit'] == 'mm')
				$data1['cbm'] = $data1['cbm']/1000000000;
			$articleDomain = new ArticleDomain($data1);
			$articlerServices->createArticle($context, $result, $articleDomain);
			if($result->isFailed())
				echo "<script>alert('".$result->getMessage()."');window.location='index.php?func=article&opt=list';</script>";
			else
			{
				$articlerpriceServices->createArticlePrice($context, $result, $articlepriceDomain);
				echo "<script>alert('Insert Success');window.location='index.php?func=article&opt=list';</script>";
			}
				
		}
		else
		header("Location:index.php?func=dash");
		break;
			
	case "delete":
		if($permit == true)
		{
			if(isset($_GET['id']))
			{
				$articlerServices->deleteArticle($context, $result,  $_GET['id']);
				header("Location:index.php?func=article&opt=list");
			}
		}
		else
		header("Location:index.php?func=dash");
		break;
			
	case "edit":
		if($permit == true)
		{
			$data = array();
			$data1 = array();
			$data2 = array();
			$dataFile = array();
			$changePrice = false;
			$uploaddir = "./images/imgArticle/";
			$uploaddirfile = "./images/drawingFile/";
			$article = $articlerServices->getArticleByOid($context, $result, $_POST['ID']);
			foreach ($_FILES as $key => $file) {
				$dataFile[$key] = $file;
			}
			$nameImg0 = $dataFile['imagePath0']['name'];
			$nameImg1 = $dataFile['imagePath1']['name'];
			$nameImg2 = $dataFile['imagePath2']['name'];
			$nameImg3 = $dataFile['imagePath3']['name'];
			$nameFile = $dataFile['specificationFile']['name'];
			if($nameImg0 == '' && $nameImg1 == '' && $nameImg2 == '' && $nameImg3 == '' && $nameFile =='')
			{
				foreach ($_POST as $key => $value) {
					$data[$key] = $value;
				}
				$data['inPriceUSD'] = Tool::convertMoneyToNumber($data['inPriceUSD']);
				$data['priceUSD'] = Tool::convertMoneyToNumber($data['priceUSD']);
				$data['inPriceEUR'] = Tool::convertMoneyToNumber($data['inPriceEUR']);
				$data['priceEUR'] = Tool::convertMoneyToNumber($data['priceEUR']);
				
				if($data['inPriceUSD'] != $article->getArticleDomain()->getInPriceUSD() || $data['priceUSD'] != $article->getArticleDomain()->getPriceUSD()
					||	$data['inPriceEUR'] != $article->getArticleDomain()->getInPriceEUR() || $data['priceEUR'] != $article->getArticleDomain()->getPriceEUR())
				{
					$changePrice = true;
					$data2['articleID'] = $_POST['code'];
					$data2['buyPriceUSD'] = Tool::convertMoneyToNumber($data['inPriceUSD']);
					$data2['sellPriceUSD'] = Tool::convertMoneyToNumber($data['priceUSD']);
					$data2['buyPriceEUR'] = Tool::convertMoneyToNumber($data['inPriceEUR']);
					$data2['sellPriceEUR'] = Tool::convertMoneyToNumber($data['priceEUR']);
					print_r($data2);					
				}
				
				$data['imagePath0'] = $article->getArticleDomain()->getImagePath0();
				$data['imagePath1'] = $article->getArticleDomain()->getImagePath1();
				$data['imagePath2'] = $article->getArticleDomain()->getImagePath2();
				$data['imagePath3'] = $article->getArticleDomain()->getImagePath3();
				$data['specificationFile'] = $article->getArticleDomain()->getSpecificationFile();
				$data['cbm'] = $data['width'] * $data['height'] * $data['depth'];
				if($data['whdUnit'] == 'dm')
					$data['cbm'] = $data['cbm']/1000;
				if($data['whdUnit'] == 'cm')
					$data['cbm'] = $data['cbm']/1000000;
				if($data['whdUnit'] == 'mm')
					$data['cbm'] = $data['cbm']/1000000000;
				$articleDomain = new ArticleDomain($data);
				$articlerServices->updateArticle($context, $result, $articleDomain);
				if($result->isSuccess() && $changePrice == true)
				{
					$articlepriceDomain = new ArticlepriceDomain($data2);
					$articlerpriceServices->createArticlePrice($context, $result, $articlepriceDomain);
				}
				header("Location:index.php?func=article&opt=edit&id=".$_POST['ID']);
			}
			else
			{
				$imagePath0 = $article->getArticleDomain()->getImagePath0();
				$imagePath1 = $article->getArticleDomain()->getImagePath1();
				$imagePath2 = $article->getArticleDomain()->getImagePath2();
				$imagePath3 = $article->getArticleDomain()->getImagePath3();
				for($i=0;$i<4;$i++)
				{
					$nameImg = $dataFile['imagePath'.$i]['name'];
					$typeImg = $dataFile['imagePath'.$i]['type'];
					$sizeImg = $dataFile['imagePath'.$i]['size'];
					
					if($sizeImg.$i > 5120000)
					{
						echo "<script>alert('Size '.$nameImg.$i.' is max');</script>";
					}
					elseif($nameImg != '')
					{
						if(($typeImg =='image/bmp' || $typeImg == 'image/jpeg' || $typeImg =='image/gif'|| $typeImg =='image/pjpeg' || $typeImg =='image/png' || $typeImg =='image/jpg' ))
						{
							$folder = $_POST['code'];
							if(!file_exists($folder))
							{
								mkdir($uploaddir.$folder);
								chmod($uploaddir.$folder, "0777");
							}
							$now = date('YmdHis');
							$photo = strstr($nameImg,".");
							$pic = $now.$i.$photo;
							$file = $uploaddir . $folder . "/" . $pic;
							move_uploaded_file($dataFile['imagePath'.$i]['tmp_name'], $file);
							$data1['imagePath'.$i] = $pic;
						}
						else
						{
							echo "<script>alert('File ' .$nameImg.$i.' is not valid');</script>";
						}
					}
				}
				$specificationFile = $article->getArticleDomain()->getSpecificationFile();
				$typeFile = $dataFile['specificationFile']['type'];
				$sizeFile = $dataFile['specificationFile']['size'];
				if($sizeFile > 10240000)
				{
					echo "<script>alert('Size '.$nameFile .' is max');</script>";
				}
				elseif($nameFile != '')
				{				
					$now = date('YmdHis');
					$photo = strstr($nameFile,".");
					$pic = $now.$i.$photo;
					$file = $uploaddirfile.$_POST['code'] . "/" . $pic;
					move_uploaded_file($dataFile['specificationFile']['tmp_name'], $file);
					$data1['specificationFile'] = $pic;
				}
				if($nameImg0 != '')
				{
					if($imagePath0 != '')
					{
						unlink($uploaddir . $_POST['code'] . "/" . $article->getArticleDomain()->getImagePath0());
					}
				}
				else
					$data1['imagePath0'] = $article->getArticleDomain()->getImagePath0();
				if($nameImg1 != '')
				{
					if($imagePath1 != '')
					{
						unlink($uploaddir . $_POST['code'] . "/" . $article->getArticleDomain()->getImagePath1());
					}
				}
				else
					$data1['imagePath1'] = $article->getArticleDomain()->getImagePath1();
				if($nameImg2 != '')
				{
					if($imagePath2 != '')
					{
						unlink($uploaddir . $_POST['code'] . "/" . $article->getArticleDomain()->getImagePath2());
					}
				}
				else
					$data1['imagePath2'] = $article->getArticleDomain()->getImagePath2();
				if($nameImg3 != '')
				{
					if($imagePath3 != '')
					{
						unlink($uploaddir . $_POST['code'] . "/" . $article->getArticleDomain()->getImagePath3());
					}
				}
				else
					$data1['imagePath3'] = $article->getArticleDomain()->getImagePath3();
				if($nameFile != '')
				{
					if($specificationFile != '')
					{
						unlink($uploaddirfile . $_POST['code'] . "/" . $article->getArticleDomain()->getSpecificationFile());
					}
				}
				else
					$data1['specificationFile'] = $article->getArticleDomain()->getSpecificationFile();
				foreach ($_POST as $key => $value) {
					$data1[$key] = $value;
				}
				$data1['inPriceUSD'] = Tool::convertMoneyToNumber($data1['inPriceUSD']);
				$data1['priceUSD'] = Tool::convertMoneyToNumber($data1['priceUSD']);
				$data1['inPriceEUR'] = Tool::convertMoneyToNumber($data1['inPriceEUR']);
				$data1['priceEUR'] = Tool::convertMoneyToNumber($data1['priceEUR']);
				
				if($data['inPriceUSD'] != $article->getArticleDomain()->getInPriceUSD() || $data['priceUSD'] != $article->getArticleDomain()->getPriceUSD()
					||	$data['inPriceEUR'] != $article->getArticleDomain()->getInPriceEUR() || $data['priceEUR'] != $article->getArticleDomain()->getPriceEUR())
				{
					$changePrice = true;
					$data2['articleID'] = $_POST['code'];
					$data2['buyPriceUSD'] = Tool::convertMoneyToNumber($data1['inPriceUSD']);
					$data2['sellPriceUSD'] = Tool::convertMoneyToNumber($data1['priceUSD']);
					$data2['buyPriceEUR'] = Tool::convertMoneyToNumber($data1['inPriceEUR']);
					$data2['sellPriceEUR'] = Tool::convertMoneyToNumber($data1['priceEUR']);					
				}
				
				$data1['cbm'] = $data1['width'] * $data1['height'] * $data1['depth'];
				if($data1['whdUnit'] == 'dm')
					$data1['cbm'] = $data1['cbm']/1000;
				if($data1['whdUnit'] == 'cm')
					$data1['cbm'] = $data1['cbm']/1000000;
				if($data1['whdUnit'] == 'mm')
					$data1['cbm'] = $data1['cbm']/1000000000;
				$articleDomain = new ArticleDomain($data1);
				$articlerServices->updateArticle($context, $result, $articleDomain);
				if($result->isSuccess() && $changePrice == true)
				{
					$articlepriceDomain = new ArticlepriceDomain($data2);
					$articlerpriceServices->createArticlePrice($context, $result, $articlepriceDomain);
				}
				header("Location:index.php?func=article&opt=edit&id=".$_POST['ID']);
			}
		}
		else
		header("Location:index.php?func=dash");
		break;
			
	case "addColor":
		if($permit == true)
		{
			$nameImg = $_FILES['file']['name'];
			$size = $_FILES['file']['size'];
			$type = $_FILES['file']['type'];
			$uploaddir = "./images/imgColor/";
			$data = array();
			$success = true;
			if($_FILES['file']['name'] != '')
			{
				if(($type =='image/bmp' || $type == 'image/jpeg' || $type =='image/gif'|| $type =='image/pjpeg' || $type =='image/png' || $type =='image/jpg' ))
				{
					$now = date('YmdHis');
					$photo = strstr($nameImg,".");
					$pic = $now.$photo;
					$file = $uploaddir . $pic;
					move_uploaded_file($_FILES['file']['tmp_name'], $file);
					$data['imagePath'] = $pic;
					$success = true;
				}
				else
				{
					echo "File is not valid <br />";
					$success = false;
				}
			}
			else
			$data['imagePath'] = '';
			if($size > 20000)
			{
				echo "Size Image is max <br />";
				$success = false;
			}
			if($_POST['colourCode'] == '')
			{
				echo "Please enter Code <br />";
				$success = false;
			}
			if($_POST['description'] == '')
			{
				echo "Please enter Description <br />";
				$success = false;
			}
			if($success == true)
			{
				$data['colourCode'] = $_POST['colourCode'];
				$data['description'] = $_POST['description'];
				$data['remarks'] = $_POST['remarks'];
				$data['type'] = (int)$_POST['type'];

				$colorService = new ColourServices();
				$colorDomain = new ColourDomain($data);
				$colorService->createColor($context, $result, $colorDomain);
				header("Location:index.php?func=article&opt=colorList");
			}
		}
		else
		header("Location:index.php?func=dash");
		break;
			
	case "editColor":
		if($permit == true)
		{
			$data = array();
			$oldImg = $_POST['imagePath'];
			$success = true;
			if(isset($_FILES['file']))
			{
				$nameImg = $_FILES['file']['name'];
				$size = $_FILES['file']['size'];
				$type = $_FILES['file']['type'];
				$uploaddir = "./images/imgColor/";
				if($_FILES['file']['name'] != '')
				{
					if(($type =='image/bmp' || $type == 'image/jpeg' || $type =='image/gif'|| $type =='image/pjpeg' || $type =='image/png' || $type =='image/jpg' ))
					{
						$now = date('YmdHis');
						$photo = strstr($nameImg,".");
						$pic = $now.$photo;
						$file = $uploaddir . $pic;
						move_uploaded_file($_FILES['file']['tmp_name'], $file);
						if($oldImg != '')
						{
							unlink($uploaddir . $oldImg);
						}
						$data['imagePath'] = $pic;
						$success = true;
					}else {
						echo "File is not valid <br />";
						$success = false;
					}
					if($size > 20000)
					{
						echo "Size Image is max <br />";
						$success = false;
					}
				}else $data['imagePath'] = '';
			}
			else $data['imagePath'] = $oldImg;
			if($success == true)
			{
				$data['colourCode'] = $_POST['colourCode'];
				$data['description'] = $_POST['description'];
				$data['remarks'] = $_POST['remarks'];
				$data['type'] = (int)$_POST['type'];
				$data['ID'] = $_POST['ID'];
				$colorService = new ColourServices();
				$colorDomain = new ColourDomain($data);
				$colorService->updateColor($context, $result, $colorDomain);
				header("Location:index.php?func=article&opt=colorList");
			}
		}
		else
		header("Location:index.php?func=dash");
		break;
			
	case "deleteColor":
		if($permit == true)
		{
			if(isset($_GET['id']))
			{
				$colorService = new ColourServices();
				$colorService->deleteByOid($context, $result, $_GET['id']);
				header("Location:index.php?func=article&opt=colorList");
			}
		}
		else
		header("Location:index.php?func=dash");
		break;

	
	case "addLine":
		if($permit == true)
		{
			$success = true;
			$data = array();
			if($_POST['name'] == '')
			{
				echo "Please enter name <br />";
				$success = false;
			}
			if($_POST['remarks'] == '')
			{
				echo "Please enter remarks";
				$success = false;
			}
			if($success == true)
			{
				foreach ($_POST as $key => $value) {
					$data[$key] = $value;
				}
				$lineDomain = new LineDomain($data);
				$lineService = new LineServices();
				$lineService->createLine($context, $result, $lineDomain);
				header("Location:index.php?func=article&opt=lineList");
			}
		}
		else
		header("Location:index.php?func=dash");
		break;
			
	case "editLine":
		if($permit == true)
		{
			$data = array();
			foreach ($_POST as $key => $value) {
				$data[$key] = $value;
			}
			$lineDomain = new LineDomain($data);
			$lineService = new LineServices();
			$lineService->updateLine($context, $result, $lineDomain);
				
			header("Location:index.php?func=article&opt=lineList");
		}
		else
		header("Location:index.php?func=dash");
		break;
			
	case "deleteLine":
		if($permit == true)
		{
			if(isset($_GET['id']))
			{
				echo "case: ".$type." action : deleteLine and id: ".$_GET['id'];
				$lineService = new LineServices();
				$lineService->deleteByOid($context, $result, $_GET['id']);
				header("Location:index.php?func=article&opt=lineList");

			}
			else
			{
				echo "case: ".$type." action : delete  ";
			}
		}
		else
		header("Location:index.php?func=dash");
		break;

	case "addAging":
		if($permit == true)
		{	
			foreach ($_POST as $key => $value) {
				$data[$key] = $value;
			}
			$agingDomain = new AgingDomain($data);
			$agingService = new AgingServices();
			$agingService->createAging($context,$result,$agingDomain);
			header("Location:index.php?func=article&opt=agingList");
		}
		else
		header("Location:index.php?func=dash");
		break;
			
	case "editAging":
		if($permit == true)
		{
			$data = array();
			foreach ($_POST as $key => $value) {
				$data[$key] = $value;
			}
			$agingDomain = new AgingDomain($data);
			$agingService = new AgingServices();
			$agingService->updateAging($context, $result, $agingDomain);
				
			header("Location:index.php?func=article&opt=agingList");
		}
		else
		header("Location:index.php?func=dash");
		break;
			
	case "deleteAging":
		if($permit == true)
		{
			if(isset($_GET['id']))
			{
				$agingService = new AgingServices();
				$agingService->deleteByOid($context, $result, $_GET['id']);
				header("Location:index.php?func=article&opt=agingList");

			}
			else
			{
				echo "case: ".$type." action : delete  ";
			}
		}
		else
		header("Location:index.php?func=dash");
		break;
	case "addWood":
		if($permit == true)
		{
			$success = true;
			$data = array();
			if($_POST['wood'] == '')
			{
				echo "Please enter Wood <br />";
				$success = false;
			}

			if($success == true)
			{
				foreach ($_POST as $key => $value) {
					$data[$key] = $value;
				}
				$woodDomain = new WoodDomain($data);
				$woodService = new WoodServices();
				$woodService->createWood($context, $result, $woodDomain);
				header("Location:index.php?func=article&opt=woodList");
			}
		}
		else
		header("Location:index.php?func=dash");
		break;
			
	case "editWood":
		if($permit == true)
		{
			$data = array();
			foreach ($_POST as $key => $value) {
				$data[$key] = $value;
			}
			$woodDomain = new WoodDomain($data);
			$woodService = new WoodServices();
			$woodService->updateWood($context, $result, $woodDomain);
			header("Location:index.php?func=article&opt=woodList");
		}
		else
		header("Location:index.php?func=dash");
		break;
			
	case "deleteWood":
		if($permit == true)
		{
			if(isset($_GET['id']))
			{
				echo "case: ".$type." action : deleteWood and id: ".$_GET['id'];
				$woodService = new WoodServices();
				$woodService->deleteByOid($context, $result, $_GET['id']);
				header("Location:index.php?func=article&opt=woodList");
			}
		}
		else
		header("Location:index.php?func=dash");
		break;

	default:
		echo "khong co tinh nang nay";
		break;
}
?>